package zi.fu.fan.zhuan;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/**
 * Created by Admin on 1/4 0004.
 */
public class StringBufferTest {
    public static void main(String[] args) {

    }

    @Test
    public void test() {
        //利用数组
        String str = "abcdefghijklmnopqrstuvwxyz";
        char[] chars = str.toCharArray();
        char[] c = new char[chars.length];
        //  for(int i=0;i<chars.length;i++){
        for (int i = 0; i <= chars.length - 1; i++) {
            c[i] = chars[chars.length - 1 - i];
        }
        System.out.print(new String(c));
    }

    @Test
    public void test1() {
        //利用StringBuffer的append方法的特点;
        String str = "abcdefghijklmnopqrstuvwxyz";
        char[] chars = str.toCharArray();
        StringBuffer sb = new StringBuffer(chars.length);
        // StringBuilder sb1=new StringBuilder(chars.length);
        for (int i = 0; i <= chars.length - 1; i++) {
            sb.append(chars[chars.length - i - 1]);
        }
        System.out.println(sb);
    }

    @Test
    public void test2() {
        //折半
        String str = "abcdefghijklmnopqrstuvwxyz";
        char[] chars = str.toCharArray();
        for (int i = 0; i <= (chars.length - 1) / 2; i++) {
            char c = chars[chars.length - 1 - i];
            chars[chars.length - 1 - i] = chars[i];
            chars[i] = c;
        }
        System.out.println(new String(chars));
    }

    @Test
    public void test4() {
        //异或运算符
        String str = "abcdefghijklmnopqrstuvwxyz";
        char[] chars = str.toCharArray();
        int l = chars.length - 1;
        for (int i = 0; i <= chars.length - 1; i++) {
            chars[i] = (char) (chars[i] ^ chars[chars.length - 1]);
            chars[chars.length - 1] = (char) (chars[i] ^ chars[chars.length - 1]);
            chars[i] = (char) (chars[i] ^ chars[chars.length - 1]);
            l--;
        }
        System.out.println(new String(chars));
    }

    @Test
    public void test5() {
        StringBuffer bf = new StringBuffer();
        char[] chars = {'1', 's', 'd', 'f', 'r', 'w', 's', 't'};
        StringBuffer bf1 = new StringBuffer(String.valueOf(chars));
        StringBuffer bf2 = new StringBuffer(100);
        StringBuffer bf3 = new StringBuffer(new String("derwerwersdfswesdf"));
        System.out.println("leng: " + bf.length() + "------capacity: " + bf.capacity());
        //leng: 0------capacity: 16
        System.out.println("leng: " + bf1.length() + "------capacity: " + bf1.capacity());
        //leng: 8------capacity: 24
        System.out.println("leng: " + bf2.length() + "------capacity: " + bf2.capacity());
        //leng: 0------capacity: 100
        System.out.println("leng: " + bf3.length() + "------capacity: " + bf3.capacity());
        //leng: 18------capacity: 34
    }

    public void sop(Object obj) {
        System.out.println(obj);
    }

    @Test
    public void test6() {
        //检查空字符串
        String str = "";
        String str1 = "  ";
        String str2 = " qwresdfasdf ";
        String str3 = "qwresdfasdf1234234dfgsd";
        String str4 = "qwresdfasdf1234234dfgsd\n";
        sop(StringUtils.isBlank(str));
        sop(StringUtils.isBlank(str1));
        sop(StringUtils.isBlank(str2));

        /*
        清除空白字符串StringUtils.trimToNull(testString)
        清除首尾的空白字符串,如果字符串是由空白字符串组成返回结果为null;
        StringUtils.trim(testString)和StringUtils.trimToEmpty();返回的结果为空白字符
         */
        sop(StringUtils.trimToNull(str));
        sop(StringUtils.trimToNull(str1));
        sop(StringUtils.trimToNull(str2));
        sop(StringUtils.trimToEmpty(str));

        /*
       取得字符串的缩写
       StringUtils.abbreviate(testString,width);
       StringUtils.abbreviate(testString,offset，width)
         */
        sop(StringUtils.abbreviate(str3, 4));//q...
        sop(StringUtils.abbreviate(str3, 2, 7));//qwre...

        /*
        劈分字符串
            StringUtils.split(testString,splitChars,arrayLength)
        splitChars中可以包含一系列的字符串来劈分testString,并可以设定得
        到数组的长度.注意设定长度arrayLength和劈分字符串间有抵触关系,建议一般情况下
        不要设定长度.
         */
        String input = "A b,c.d|e";
        String input2 = "Pharmacy, basketball funky";
        String[] array1 = StringUtils.split(input, " ,.|");
        String[] array2 = StringUtils.split(input2, " ,", 2);

        sop(ArrayUtils.toString(array1));//{A,b,c,d,e}
        sop(ArrayUtils.toString(array2));//{Pharmacy,basketball funky}

        /*
        查找嵌套的字符串
        StringUtils.substringBetween(testString,header,tail)
        在testString中取得header和tail之间的字符串。不存在则返回空
         */
        sop(StringUtils.substringBetween(str3, "123", "sd"));//4234dfg
        sop(StringUtils.substringBetween(str3, "1236", "sd"));//null

        /*
        去除尾部换行符
        StringUtils.chomp(testString): 去除testString尾部的换行符
         */
        sop(StringUtils.chomp(str4));
        /*
        重复字符串
        StringUtils.repeat(repeatString,count): 得到将repeatString重复count次后的字符串
        StringUtils.center( testString, count,repeatString ):
        把testString插入将repeatString重复多次后的字符串中间,得到字符串的总长为count
         */
        String str5 = "q";
        sop(StringUtils.repeat(str5, 10));//qqqqqqqqqq
        sop(StringUtils.center("你好", 10, str5));//qqqq你好qqqq

        /*
           颠倒字符串
           StringUtils.reverse(str);
           得到str 中字符串颠倒后的字符串
           public static String reverse(String str) {
                return str == null?null:(new StringBuffer(str)).reverse().toString();
            }
         */
        sop(StringUtils.reverse(str4));//dsgfd4324321fdsafdserwq

        /*
            取得某一个字符串在另一个字符串中出现的次数
            StringUtils.countMatches(testString,seqString):
            取得seqString在testString中出现的次数,未发现则返回零
         */
        sop(StringUtils.countMatches(str4,"1"));

        /*
            部分截取字符串
            StringUtils.substringBetween(testString,fromString,toString ):取得两字符之间的字符串
         */
        String str6 = " 25 * (30,40) [50,60] | 30";
        sop("N0: " + StringUtils.substringBeforeLast( str6, "*" ) );
        sop(", N1: " + StringUtils.substringBetween( str6,"(", "," ) );
        sop(", N2: " + StringUtils.substringBetween( str6,",", ")" ) );
        sop(", N3: " + StringUtils.substringBetween( str6,"[", "," ) );
        sop(", N4: " + StringUtils.substringBetween( str6,",", "]" ) );
        sop(", N5: " + StringUtils.substringAfterLast(str6,"|" ) );
        // N0:  25 , N1: 30, N2: 40, N3: 50, N4: 40) [50,60, N5:  30


        /*
        首字母大,小写:
        static String capitalize(String str) 首字母大写
         static String uncapitalize(String str)首字母小写
     */
        sop(StringUtils.capitalize("abcd"));//Abcd
        sop(StringUtils.uncapitalize("ABCD"));//aBCD

        /*
            是否全是大写，是否全是小写
             public static boolean isAllLowerCase(String str);
             public static boolean isAllUpperCase(String str);
         */
        /*
        是否都是由字母组成：
             public static boolean isAlpha(String str);  只由字母组成
             public static boolean isAlphaSpace(String str); 只有字母和空格组成
             public static boolean isAlphanumeric(String str);只由字母和数字组成
             public static boolean isAlphanumericSpace(String str);只由字母数字和空格组成
             public static boolean isNumeric(String str);只由数字组成
             public static boolean isNumericSpace(String str);只由数字和空格组成
         */

        /*
        字符串比较
        StringUtils.equals();
        StringUtils.equalsIgnoreCase(); 忽略大小写
         */

        /*
        查找
        StringUtils.indexOf();返回所在位置
         */
        String str7="qwrqwerqrq";
        sop(StringUtils.lastIndexOf(str7,"q"));

    }

}
